/**
 * ZProblem.java
 * com.zhihu.zixun.model
 * Copyright (c) 2016, 北京聚智未来科技有限公司版权所有.
*/

package com.zhihu.zixun.model;

import java.util.List;

import com.eova.common.base.BaseModel;
import com.jfinal.plugin.activerecord.Db;

/**
 * 问题
 * @author   wxl
 * @Date	 2016年12月23日 	 
 */
public class ZProblem extends BaseModel<ZProblem> {

	/**
	 * TODO（用一句话描述这个变量表示什么）
	 */

	private static final long serialVersionUID = 2712614989558153005L;

	public static final ZProblem dao = new ZProblem();

	/**
	 * 查询
	 * 咨询列表
	 * 分类-问题-第一条回答-回答的浏览量
	 * @param id
	 */
	public List<ZProblem> queryAll(String id) {
		String sql = "SELECT c.title,p.id as problemid,p.title,p.content,p.createDate,"
				+ "(SELECT u.nickname from c_user u where u.id=p.createUser) as creatername,"
				+ "(SELECT u.headImgUrl from c_user u where u.id=p.createUser) as headimgurl,"
				+ "p.isDelete,p.isRecommend,a.*"
				+ "FROM c_category c INNER JOIN z_problem p ON c.id=p.classType "
				+ "INNER JOIN z_answer a on a.problemId=p.id WHERE c.id=? and "
				+ "(SELECT count(*) FROM z_problem p1 INNER JOIN z_answer a1 ON p1.id=a1.problemId INNER JOIN c_category c1 ON c1.id=p1.classType "
				+ "WHERE a1.createDate>a.createDate AND p1.id =p.id)<1";
		return this.find(sql, id);
	}

	/**
	 * 查询
	 * 问题信息
	 * @param id
	 */
	public ZProblem queryProblem(String id) {
		String sql = "SELECT p.title,p.content,p.lookNo FROM z_problem p WHERE p.id=?";
		return this.findFirst(sql, id);
	}

	/**
	 * 阅读量加一
	 * @param id
	 */
	public int updateLookNo(String id) {
		String sql = "UPDATE z_problem p SET p.lookNo = p.lookNo+1 where p.id = ?";
		int iRet = Db.update(sql, id);// return 1 success
		return iRet;

	}

}
